package com.yy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.entity.Invoice;
import com.yy.mapper.InvoiceMapper;
import com.yy.service.InvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Override
    public Integer save(Invoice invoice) {
        int insert = invoiceMapper.insert(invoice);
        return insert;
    }

    @Override
    public String selectByDate(String date) {
        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
        log.info("**********************************");
        log.info("date:" + date);
        invoiceQueryWrapper.like("invoice_date",date);
        List<Invoice> invoices = invoiceMapper.selectList(invoiceQueryWrapper);
        BigDecimal bigDecimal = new BigDecimal("0.00");
        for (Invoice invoice : invoices) {
            bigDecimal = bigDecimal.add(new BigDecimal(invoice.getTotalAmount()));
        }
            log.info("**************************************");
            log.info("bigDecimal:" + bigDecimal.toString());
        return bigDecimal.toString();
    }

}
